package com.atguigu.aclservice.service.impl;

import com.atguigu.aclservice.entity.Menu;
import com.atguigu.aclservice.entity.RoleMenu;
import com.atguigu.aclservice.entity.vo.AssginMenuVo;
import com.atguigu.aclservice.entity.vo.RouterVo;
import com.atguigu.aclservice.mapper.MenuMapper;
import com.atguigu.aclservice.mapper.RoleMenuMapper;
import com.atguigu.aclservice.service.MenuService;
import com.atguigu.aclservice.service.UserRoleService;
import com.atguigu.aclservice.service.UserService;
import com.atguigu.aclservice.util.MenuHelper;
import com.atguigu.aclservice.util.RouterHelper;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author fxy
 * @since 2022-10-27
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 菜单列表树形列表
     * @return
     */
    @Override
    public List<Menu> findNodes() {
        //获取所有菜单
        List<Menu> menuList = baseMapper.selectList(null);
        //所有菜单数据转换要求数据类型
        List<Menu> resultList = MenuHelper.buildTree(menuList);

        return resultList;
    }

    @Override
    public void removeMenuById(Long id) {
        //查询当前菜单下是否有子菜单
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        Integer integer = baseMapper.selectCount(wrapper);
        if (integer>0){
            //有菜单
            throw new GuliException(20001,"请先删除子菜单");
        }
        //删除
        baseMapper.deleteById(id);
    }

    @Override
    public List<Menu> findMenuByRoleId(Long roleId) {
        //获取所有菜单 status=1
        QueryWrapper<Menu> wrapperMenu = new QueryWrapper<>();
        wrapperMenu.eq("status",1);
        List<Menu> menuList = baseMapper.selectList(wrapperMenu);

        //根据角色id查询 角色分配过的菜单列表
        QueryWrapper<RoleMenu> wrapperRoleMenu = new QueryWrapper<>();
        wrapperRoleMenu.eq("role_id",roleId);
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(wrapperRoleMenu);

        //从第二步查询列表中，获取角色分配所有菜单id
        List<Long> roleMenuIds = new ArrayList<>();
        for (RoleMenu sysRoleMenu:roleMenus) {
            Long menuId = sysRoleMenu.getMenuId();
            roleMenuIds.add(menuId);
        }

        //数据处理：isSelect 如果菜单选中 true，否则false
        // 拿着分配菜单id 和 所有菜单比对，有相同的，让isSelect值true
        for (Menu sysMenu : menuList) {
            if(roleMenuIds.contains(sysMenu.getId())) {
                sysMenu.setIsSelect(true);
            } else {
                sysMenu.setIsSelect(false);
            }
        }

        //转换成树形结构为了最终显示 MenuHelper方法实现
        List<Menu> sysMenus = MenuHelper.buildTree(menuList);
        return sysMenus;
    }

    //根据角色分配菜单
    @Override
    public void doAssign(AssginMenuVo assginMenuVo) {
        //根据角色id删除菜单权限
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",assginMenuVo.getRoleId());
        roleMenuMapper.delete(wrapper);

        //遍历菜单id列表，一个一个进行添加
        List<Long> menuIdList = assginMenuVo.getMenuIdList();
        for (Long menuId:menuIdList) {
            RoleMenu sysRoleMenu = new RoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(assginMenuVo.getRoleId());
            roleMenuMapper.insert(sysRoleMenu);
        }
    }

    @Override
    public List<RouterVo> getUserMenuList(Long id) {
        //查询是不是超级管理员
        List<Menu> menuList=null;
        if (id==1){
            QueryWrapper<Menu> wrapper=new QueryWrapper<>();
            wrapper.eq("status",1);
            wrapper.orderByAsc("id");
            menuList=baseMapper.selectList(wrapper);
        }else {
            menuList=baseMapper.findListUserId(id);
        }
        List<Menu> treeList = MenuHelper.buildTree(menuList);
        //转换成前端路由
        List<RouterVo> routerVoList = RouterHelper.buildRouters(treeList);

        return routerVoList;
    }

    @Override
    public List<String> getUserButtonList(Long id) {
        //查询是不是超级管理员
        List<Menu> menuList=null;
        if (id==1){
            QueryWrapper<Menu> wrapper=new QueryWrapper<>();
            wrapper.eq("status",1);
            menuList=baseMapper.selectList(wrapper);
        }else {
            menuList=baseMapper.findListUserId(id);
        }

        List<String> permissionList=new ArrayList<>();
        for (Menu menu : menuList) {
            //type=2
            if (menu.getType()==2){
                String perms = menu.getPerms();
                permissionList.add(perms);
            }
        }
        return permissionList;

    }
}
